package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.BaiduAiUtils;
import cn.itsource.basic.util.Md5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.domain.ShopDto;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：店铺
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        String address = shop.getAddress();
        String name = shop.getName();String tel = shop.getTel();
        //一：校验
        //1.校验空值
        if(StringUtils.isEmpty(address) || StringUtils.isEmpty(name) || StringUtils.isEmpty(tel) ){
            throw new BusinessException("数据不能为空");
        }
        //2.校验密码是否一致[思路：在domain中加一个参数就可以接收确认密码]
        //3.校验店铺是否已经入驻过
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if(dbShop != null){//入驻过
            throw new BusinessException("该店铺已入驻，请直接登录。忘记密码请联系管理员!!!");
        }
        //4.智能审核 - 百度AI
        //审核文本
        if(!BaiduAiUtils.textCensor(shop.getName())){
            throw new BusinessException("店铺名称非法!!!");
        }
        //审核图片
        String imgUrl = "http://123.207.27.208" + shop.getLogo();
        if(!BaiduAiUtils.imgCensor(imgUrl)){
            throw new BusinessException("店铺logo非法!!!");
        }

        //二：保存数据
        //1.保存店铺管理员信息
        Employee admin = shop.getAdmin();
        //设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        admin.setSalt(salt);
        //设置密码
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setPassword(md5Pwd);

        //保存logininfo的信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//设置管理员
        logininfoMapper.save(logininfo);
        //将logininfo_id设置到employee中
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.save(admin); //这一步执行完就有employee_id

        //2.保存店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);//这一步执行完就有shop_id
        //3.将shopId设置到employee中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException { //shopId,note
        //1.改状态：1 - 2
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(325L); //写死的，因为现在没有登录，不知道是哪一个管理员审核的
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送激活邮件 @TODO
        //创建复杂的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发送方
        helper.setFrom("1104829900@qq.com");
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //设置收件人
        helper.setTo(employee.getEmail());
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h2>店铺激活邮件<br/><a href='http://localhost:8080/shop/active/"
                +shop.getId()+"'>点击链接激活</a></h2>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //1.改状态：1 - 4
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送邮件重新入驻 @TODO
        //创建复杂的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发送方
        helper.setFrom("1104829900@qq.com");
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //设置收件人
        helper.setTo(employee.getEmail());
        //设置主题
        helper.setSubject("店铺审核失败");
        //设置内容
        helper.setText("<h2>你的店铺审核失败，请重新入驻<br/>" +
                "<a href='http://localhost:8081/#/register'>点击链接重新入驻</a></h2>",true);
        //发送邮件
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        employeeMapper.remove(employee.getId());
        shopMapper.remove(shop.getId());

    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
